const { ipcRenderer } = require('electron')

class DigitalClock {
    constructor() {
        this.isAlwaysOnTop = true
        this.currentTime = {
            hour1: '',
            hour2: '',
            minute1: '',
            minute2: ''
        }
        this.previousTime = {
            hour1: '',
            hour2: '',
            minute1: '',
            minute2: ''
        }
        
        this.initializeElements()
        this.bindEvents()
        this.startClock()
        this.updateToggleButton()
    }

    initializeElements() {
        this.elements = {
            date: document.getElementById('date'),
            weekday: document.getElementById('weekday'),
            weekNumber: document.getElementById('weekNumber'),
            hour1: document.getElementById('hour1'),
            hour2: document.getElementById('hour2'),
            minute1: document.getElementById('minute1'),
            minute2: document.getElementById('minute2'),
            toggleTopBtn: document.getElementById('toggleTop'),
            closeBtn: document.getElementById('closeBtn')
        }
    }

    bindEvents() {
        this.elements.toggleTopBtn.addEventListener('click', () => {
            this.toggleAlwaysOnTop()
        })
        
        this.elements.closeBtn.addEventListener('click', () => {
            window.close()
        })
    }

    updateToggleButton() {
        if (this.isAlwaysOnTop) {
            this.elements.toggleTopBtn.classList.add('active')
            this.elements.toggleTopBtn.textContent = '取消置顶'
        } else {
            this.elements.toggleTopBtn.classList.remove('active')
            this.elements.toggleTopBtn.textContent = '置顶显示'
        }
    }

    toggleAlwaysOnTop() {
        this.isAlwaysOnTop = !this.isAlwaysOnTop
        ipcRenderer.send('toggle-always-on-top')
        this.updateToggleButton()
    }

    getBeijingTime() {
        // 获取北京时间 (UTC+8)
        const now = new Date()
        const utc = now.getTime() + (now.getTimezoneOffset() * 60000)
        const beijingTime = new Date(utc + (8 * 3600000))
        return beijingTime
    }

    getWeekNumber(date) {
        // 计算年度第几周
        const startOfYear = new Date(date.getFullYear(), 0, 1)
        const pastDaysOfYear = (date - startOfYear) / 86400000
        return Math.ceil((pastDaysOfYear + startOfYear.getDay() + 1) / 7)
    }

    formatDigit(num) {
        return num.toString().padStart(2, '0')
    }

    updateDigit(elementId, newValue, oldValue) {
        const element = this.elements[elementId]
        const topHalf = element.querySelector('.digit-top')
        const bottomHalf = element.querySelector('.digit-bottom')

        if (newValue !== oldValue && oldValue !== '') {
            // 触发翻页动画
            element.classList.add('flipping')
            
            // 设置旧值
            topHalf.textContent = oldValue
            bottomHalf.textContent = oldValue

            setTimeout(() => {
                // 动画中间设置新值
                topHalf.textContent = newValue
                bottomHalf.textContent = newValue
            }, 300)

            setTimeout(() => {
                // 移除动画类
                element.classList.remove('flipping')
            }, 600)
        } else {
            // 没有变化时或初始化时直接设置值
            topHalf.textContent = newValue
            bottomHalf.textContent = newValue
        }
    }

    updateClock() {
        const beijingTime = this.getBeijingTime()
        
        // 获取时分
        const hours = this.formatDigit(beijingTime.getHours())
        const minutes = this.formatDigit(beijingTime.getMinutes())
        
        // 分离数字
        const newTime = {
            hour1: hours[0],
            hour2: hours[1],
            minute1: minutes[0],
            minute2: minutes[1]
        }

        // 更新每个数字（如果有变化则触发翻页动画）
        this.updateDigit('hour1', newTime.hour1, this.currentTime.hour1)
        this.updateDigit('hour2', newTime.hour2, this.currentTime.hour2)
        this.updateDigit('minute1', newTime.minute1, this.currentTime.minute1)
        this.updateDigit('minute2', newTime.minute2, this.currentTime.minute2)

        // 更新日期信息
        this.updateDateInfo(beijingTime)

        // 保存当前时间
        this.currentTime = newTime
    }

    updateDateInfo(date) {
        // 格式化日期
        const year = date.getFullYear()
        const month = this.formatDigit(date.getMonth() + 1)
        const day = this.formatDigit(date.getDate())
        
        // 中文星期
        const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
        const weekday = weekdays[date.getDay()]
        
        // 年度第几周
        const weekNumber = this.getWeekNumber(date)

        // 更新显示
        this.elements.date.textContent = `${year}年${month}月${day}日`
        this.elements.weekday.textContent = weekday
        this.elements.weekNumber.textContent = `第${weekNumber}周`
    }

    startClock() {
        // 立即更新一次
        this.updateClock()
        
        // 每秒更新
        setInterval(() => {
            this.updateClock()
        }, 1000)
    }
}

// 页面加载完成后初始化时钟
document.addEventListener('DOMContentLoaded', () => {
    new DigitalClock()
})

// 阻止右键菜单
document.addEventListener('contextmenu', (e) => {
    e.preventDefault()
}) 